package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.MovieActivity;
import flc.ast.activity.MovieDetailActivity;
import flc.ast.activity.SearchActivity;
import flc.ast.adapter.HomeMovieAdapter;
import flc.ast.adapter.ScoreMovieAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private ScoreMovieAdapter scoreMovieAdapter;
    private HomeMovieAdapter recommendAdapter, classicAdapter, movieAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private int type = 0;

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.ivLast.setOnClickListener(this);
        mDataBinding.ivNext.setOnClickListener(this);
        mDataBinding.tvLatestRecommend.setOnClickListener(this);
        mDataBinding.tvClassicMovie.setOnClickListener(this);
        mDataBinding.tvType1.setOnClickListener(this);
        mDataBinding.tvType2.setOnClickListener(this);
        mDataBinding.tvType3.setOnClickListener(this);

        mDataBinding.rvScoreMovieList.setLayoutManager(new GridLayoutManager(mContext, 4));
        scoreMovieAdapter = new ScoreMovieAdapter();
        mDataBinding.rvScoreMovieList.setAdapter(scoreMovieAdapter);
        scoreMovieAdapter.setOnItemClickListener(this);

        mDataBinding.rvRecommendList.setLayoutManager(new GridLayoutManager(mContext, 3));
        recommendAdapter = new HomeMovieAdapter();
        mDataBinding.rvRecommendList.setAdapter(recommendAdapter);
        recommendAdapter.setOnItemClickListener(this);

        mDataBinding.rvClassicList.setLayoutManager(new GridLayoutManager(mContext, 3));
        classicAdapter = new HomeMovieAdapter();
        mDataBinding.rvClassicList.setAdapter(classicAdapter);
        classicAdapter.setOnItemClickListener(this);

        mDataBinding.rvMovieList.setLayoutManager(new GridLayoutManager(mContext, 3));
        movieAdapter = new HomeMovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.setOnItemClickListener(this);

        mDataBinding.refreshMovieItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshMovieItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshMovieItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                if (type == 0) {
                    getData(AppConfig.HOME_TYPE_DATA1);
                } else if (type == 1) {
                    getData(AppConfig.HOME_TYPE_DATA2);
                } else if (type == 2) {
                    getData(AppConfig.HOME_TYPE_DATA3);
                }
                mDataBinding.refreshMovieItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                if (type == 0) {
                    getData(AppConfig.HOME_TYPE_DATA1);
                } else if (type == 1) {
                    getData(AppConfig.HOME_TYPE_DATA2);
                } else if (type == 2) {
                    getData(AppConfig.HOME_TYPE_DATA3);
                }
                mDataBinding.refreshMovieItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                String key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_text));
                    return;
                }
                SearchActivity.key = key;
                startActivity(SearchActivity.class);
                break;
            case R.id.ivLast://上一页
            case R.id.ivNext://下一页
                getScoreMovieData();
                break;
            case R.id.tvLatestRecommend://最新推荐
                MovieActivity.type = 0;
                startActivity(MovieActivity.class);
                break;
            case R.id.tvClassicMovie://经典泰剧
                MovieActivity.type = 1;
                startActivity(MovieActivity.class);
                break;
            case R.id.tvType1://按时间
                clearSelection();
                type = 0;
                mDataBinding.tvType1.setBackgroundColor(Color.parseColor("#0181FE"));
                mDataBinding.tvType1.setTextColor(Color.parseColor("#FFFFFF"));
                getData(AppConfig.HOME_TYPE_DATA1);
                break;
            case R.id.tvType2://按人气
                clearSelection();
                type = 1;
                mDataBinding.tvType2.setBackgroundColor(Color.parseColor("#0181FE"));
                mDataBinding.tvType2.setTextColor(Color.parseColor("#FFFFFF"));
                getData(AppConfig.HOME_TYPE_DATA2);
                break;
            case R.id.tvType3://按高分
                clearSelection();
                type = 2;
                mDataBinding.tvType3.setBackgroundColor(Color.parseColor("#0181FE"));
                mDataBinding.tvType3.setTextColor(Color.parseColor("#FFFFFF"));
                getData(AppConfig.HOME_TYPE_DATA3);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        MovieDetailActivity.stkResBean = bean;
        startActivity(MovieDetailActivity.class);
    }

    @Override
    protected void initData() {
        getScoreMovieData();
        getRecommendData();
        getClassicData();
        getData(AppConfig.HOME_TYPE_DATA1);
    }

    private void getScoreMovieData() {
        StkResApi.getTagResourceList(null, AppConfig.SCORE_MOVIE_DATA, StkResApi.createParamMap(0, 4), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        scoreMovieAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getRecommendData() {
        StkResApi.getTagResourceList(null, AppConfig.LATEST_RECOMMEND_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        recommendAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getClassicData() {
        StkResApi.getTagResourceList(null, AppConfig.CLASSIC_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        classicAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getData(String url) {
        StkResApi.getTagResourceList(null, url, StkResApi.createParamMap(0, 9), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            movieAdapter.setList(result);
                        } else {
                            movieAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.tvType1.setBackgroundColor(Color.parseColor("#1E2648"));
        mDataBinding.tvType1.setTextColor(Color.parseColor("#57607F"));
        mDataBinding.tvType2.setBackgroundColor(Color.parseColor("#1E2648"));
        mDataBinding.tvType2.setTextColor(Color.parseColor("#57607F"));
        mDataBinding.tvType3.setBackgroundColor(Color.parseColor("#1E2648"));
        mDataBinding.tvType3.setTextColor(Color.parseColor("#57607F"));
    }

}